﻿using AuthCenter.Api.Write.Application.Command.Apply;
using AuthCenter.Application;
using AuthCenter.Domain;
using AuthCenter.ErrorCode;
using AutoMapper;
using MediatR;

namespace AuthCenter.Api.Write.Application.Hander.Apply
{
    public class UpdateApplyStatusHander:IRequestHandler<UpdateApplyStatusCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<ApplyModel> _applyRepository;
        private readonly IMapper _mapper;
        public UpdateApplyStatusHander(IBaseRepository<ApplyModel> applyRepository, IMapper mapper)
        {
            _applyRepository = applyRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 修改申请状态
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<int>> Handle(UpdateApplyStatusCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            
            if (request.Status == "驳回" && (request.Remark == string.Empty&&request.Remark==null)) 
            {
                res.Code = ApiEnums.Error;
                res.Message = "请填写驳回理由";
                return Task.FromResult(res);
            }
                    
            var model = _mapper.Map<ApplyModel>(request);
            
            int i = _applyRepository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "审核成功";
                res.Data = i;
            }
            else 
            {
                res.Code = ApiEnums.Fail;
                res.Message = "审核失败";
            }
            return Task.FromResult(res);
        }
    }
}
